package com.pochogastos.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jasypt.util.password.BasicPasswordEncryptor;

import com.pochogastos.dao.UsuarioDAO;
import com.pochogastos.model.Usuario;

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Login() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			if(request.getSession().getAttribute("ERROR_LOGIN")!=null)
				request.getSession().removeAttribute("ERROR_LOGIN");
			
			String accion = request.getParameter("accion");
			if(accion == null || "".equalsIgnoreCase(accion)){
				RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp");
				rd.forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/error.jsp");
			request.getSession().setAttribute("ERROR", e.getMessage());
			rd.forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String accion = request.getParameter("accion");
			String forward = "/login.jsp";
			if(request.getSession().getAttribute("ERROR_REGISTRO")!=null)
				request.getSession().removeAttribute("ERROR_REGISTRO");
			if(request.getSession().getAttribute("ERROR_LOGIN")!=null)
				request.getSession().removeAttribute("ERROR_LOGIN");
			
			if("ingresar".equalsIgnoreCase(accion)){
				String usuario = request.getParameter("usuario");
				String password = request.getParameter("password");
				Usuario u = UsuarioDAO.getInstance().login(usuario);
				if(u!=null){
					BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
					if(passwordEncryptor.checkPassword(password, u.getPassword())){
						forward="/Home";
						request.getSession().setAttribute("USUARIO", u);
					}else{
						request.getSession().setAttribute("ERROR_LOGIN","Usuario o Password incorrectos");
					}
				}else{
					request.getSession().setAttribute("ERROR_LOGIN","Usuario o Password incorrectos");
				}
			}else if("logout".equalsIgnoreCase(accion)){
				if(request.getSession().getAttribute("USUARIO")!=null)
					request.getSession().removeAttribute("USUARIO");
				forward="/login.jsp";
			}
			RequestDispatcher rd = getServletContext().getRequestDispatcher(forward);
			rd.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/error.jsp");
			request.getSession().setAttribute("ERROR", e.getMessage());
			rd.forward(request, response);
		}
	}

}
